﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.IO;
using System.Text;

namespace _2015.DivAdmin
{
    public partial class DivAdminCalender3 : System.Web.UI.Page
    {
        String email;
        String name;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ButtonInviteCandidates_Click(object sender, EventArgs e)
        {
            int InterviewNo = (int)Session["InterviewNo"];
            String InterviewDate = (String)Session["InterviewDate"];
            String BoardRoom = (String)Session["BoardRoom"];

            StringBuilder str = new StringBuilder();

            // Select the checkboxes from the GridView control
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                GridViewRow row = GridView1.Rows[i];
                bool isChecked = ((CheckBox)row.FindControl("checkSelect")).Checked;

                if (isChecked)
                {
                    int a = Convert.ToInt32(GridView1.Rows[i].Cells[1].Text);

                     string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

                     using (SqlConnection con = new SqlConnection(CS))
                     {

                             SqlCommand com = new SqlCommand("spGetInterviweeEmail", con);
                             com.CommandType = CommandType.StoredProcedure;

                             SqlParameter getemail = new SqlParameter("@GetEmail", a);
                             com.Parameters.Add(getemail);



                             con.Open();
                             SqlDataReader dr;

                             dr = com.ExecuteReader();

                             if (dr.Read())
                             {
                                 email = dr["EMAIL"].ToString();
                                 name = dr["FIRSTNAME"].ToString();
                             }
                             con.Close();
                         
                     }

                        

                        if (Page.IsValid)
                            try
                            {
                                MailMessage mm = new MailMessage("chandimacpgz2011@gmail.com", email);
                                //mm.From = new MailAddress();
                                //mm.To.Add();
                                mm.Subject = "MIT Interview Invitation";

                                mm.Body = "Hi " + name + ",/nCongratulations!.You are being selected for our company's 1st Interview.\n\nInterview No:" + InterviewNo + "\nDate:" + InterviewDate + "\nVenue:" + BoardRoom+".";

                                mm.IsBodyHtml = true;

                                SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
                                smtpClient.EnableSsl = true;
                                smtpClient.Credentials = new System.Net.NetworkCredential()
                                {
                                    UserName = "chandimacpgz2011@gmail.com",
                                    Password = "95340(%#$)95340"
                                };
                                smtpClient.Send(mm);


                            }

                            catch (Exception ex)
                            {
                                Response.Write("Could not send the e-mail - error: " + ex.Message);
                            }

                }
            }
            Response.Redirect("DivAdminInterviewCheck.aspx");
        }
    }
}